package com.example.cloudtrade.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.cloudtrade.ProductDetailActivity;
import com.example.cloudtrade.R;
import com.example.cloudtrade.SearchActivity;

public class FandFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_fand, container, false);

    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 初始化商品项点击监听
        setupProductClickListeners(view);

        // 返回按钮处理
        ImageView ivBack = view.findViewById(R.id.ivBack);
        ivBack.setOnClickListener(v -> requireActivity().onBackPressed());
        View searchContainer = view.findViewById(R.id.search_container);
        searchContainer.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), SearchActivity.class);
            startActivity(intent);
        });
    }

    private void setupProductClickListeners(View rootView) {
        int[] productIds = {R.id.product_item_1, R.id.product_item_2};
        String[] titles = {"初始账号", "游戏道具"};
        String[] prices = {"299.00", "199.00"};
        String[] dates = {"2023-12-20", "2023-12-21"};

        for (int i = 0; i < productIds.length; i++) {
            int finalI = i;
            rootView.findViewById(productIds[i]).setOnClickListener(v -> {
                Intent intent = new Intent(getActivity(), ProductDetailActivity.class);
                intent.putExtra("title", titles[finalI]);
                intent.putExtra("price", prices[finalI]);
                intent.putExtra("publishDate", dates[finalI]);
                startActivity(intent);
            });
        }
    }
}
